package com.atguigu.service_edu.controller;


import com.atguigu.common_utils.Response;
import com.atguigu.service_base.exception.GuliException;
import com.atguigu.service_edu.domain.EduTeacher;
import com.atguigu.service_edu.service.EduTeacherService;
import com.atguigu.service_edu.vo.TeacherQuery;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zhj
 * @since 2021-10-17
 */
@RestController
@RequestMapping("/service_edu/edu-teacher")
@Api("EduTeacher API 文档")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;

    @GetMapping("/selectAll")
    @ApiOperation("查询老师列表")
    public Response selectAll() {
        return Response.ok().data("item",eduTeacherService.list(null));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除操作")
    public Response deleteEduById( @ApiParam(name = "id",value = "老师ID",required = true) @PathVariable("id") String id){
        boolean isSuccess = eduTeacherService.removeById(id);
        if(isSuccess){
            return Response.ok();
        }
        return Response.err();
    }
    @GetMapping("/selectAllByPageHelper/{current}/{size}")
    @ApiOperation("查询老师列表分页查询")
    public Response selectAllByPageHelper(
            @PathVariable("current") Integer current,
            @PathVariable("size") Integer size
    ) {
        Page<EduTeacher> page = new Page<>(current, size);
        eduTeacherService.page(page,null);
        Map<String, Object> map = new HashMap<>();
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        map.put("total",total);
        map.put("item",records);
        return Response.ok().data(map);
    }
    /**
    * @Description: 多组合条件查询带分页
     *  name 模糊查询 ，是否相等 level  ， create 是否大于某个时间
    * @date 2021/10/18 下午9:58
    */
    @PostMapping("/pageTeacherHelper/{current}/{size}")
    @ApiOperation("多条件分页查询")
    public Response pageTeacherHelper(
            @PathVariable("current") Integer current,
            @PathVariable("size") Integer size,
            @RequestBody(required = false) TeacherQuery teacherQuery
            ){
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        Date createTime = teacherQuery.getCreateTime();
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name); // 注意 这里是表中的 字段名
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(createTime)){
            wrapper.ge("gmt_create",createTime);
        }
        Page<EduTeacher> page = new Page<>(current,size);
        eduTeacherService.page(page,wrapper);
        long total = page.getTotal();
        List<EduTeacher> records = page.getRecords();
        return Response.ok().data("total",total).data("item",records);

    }
    @PostMapping("/addTeacher")
    @ApiOperation("增加功能")
    public Response addTeacher(
            @RequestBody(required = false) EduTeacher eduTeacher){
        boolean save = eduTeacherService.save(eduTeacher);
        if(save){
            return Response.ok();
        }
        return Response.err();
    }

    @GetMapping("/teacher/{id}")
    @ApiOperation("查询功能")
    public Response selectTeacherById(@PathVariable("id") Integer id){
        try {
            System.out.println(10 / 0);
        } catch (Exception e) {
            throw new GuliException(20001,"触发了自定义异常");
        }
        return Response.ok().data("item", eduTeacherService.getById(id));

    }

    @PostMapping("/teacher")
    @ApiOperation("修改功能")
    public Response updateTeacher(@RequestBody(required = false) EduTeacher eduTeacher){
        boolean update = eduTeacherService.updateById(eduTeacher);
        if(update){
            return Response.ok();
        }
        return Response.err();
    }

}

